<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PromiseAll</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const myPromiseAll = promiseArr => {
            return new Promise((resolve, reject) => {
                if (!Array.isArray(promiseArr)) {
                    return reject('arguments must be an array!');
                }
                let count = 0;
                let resolveArr = [];
                const len = promiseArr.length;
                for (let i = 0; i < len; i++) {
                    Promise.resolve(promiseArr[i])
                        .then(value => {
                            count++;
                            resolveArr[i] = value;
                            if (count === len) {
                                resolve(resolveArr);
                            }
                        })
                        .catch(e => reject(e));
                }
            });
        };
        const promise1 = new Promise < number > (resolve => resolve(1));
        const promise2 = new Promise < number > (resolve => resolve(2));
        const promise3 = new Promise < number > (resolve => resolve(3));
        const promiseList = [promise1, promise2, promise3];
        const promiseAll = myPromiseAll(promiseList);
        promiseAll.then(function (res) {
            console.log(res);
        }); // [1,2,3]
    </script>
</body>

</html>